package com.ck.controller.teacher;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ck.domain.Exam;
import com.ck.domain.Subject;
import com.ck.service.ExamService;
import com.ck.service.QuestionService;

public class EditUnstartExamInfoController extends SimpleFormController {
	
	private ExamService examService;
	private QuestionService questionService;

	//设置公共变量，供回显表单时使用
	private List<Subject> subjectList = null;
	
	public void setExamService(ExamService examService) {
		this.examService = examService;
	}
	
	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Integer examID = Integer.parseInt(request.getParameter("examID"));
		Exam exam = examService.gainUnstartExam(examID);

		DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start_time_str = f.format(exam.getStart_time());
		String end_time_str = f.format(exam.getEnd_time());

		subjectList = questionService.gainSubjectList();
		
		Map model = new HashMap();
		
		model.put("start_time_str", start_time_str);
		model.put("end_time_str", end_time_str);
		model.put("subjectList", subjectList);
		
		return model;
	}
	
	protected Object formBackingObject(HttpServletRequest request)
	throws Exception {
		Integer examID = Integer.parseInt(request.getParameter("examID"));
		Exam exam = examService.gainUnstartExam(examID);
		return exam;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Integer examID = Integer.parseInt(request.getParameter("examID"));
		Exam exam = (Exam) command;
		exam.setId(examID);
		
		Map model = errors.getModel();
		
		//后台校验
		boolean flag = true;
		String title = exam.getTitle().trim();
		Integer subjectID = Integer.parseInt(request.getParameter("subject"));
		String remark = exam.getRemark().trim();
		
		String start_time_str = (String) request.getParameter("start_time_str");
		String end_time_str = (String) request.getParameter("end_time_str");

		if(title == null || "".equals(title)) {
			model.put("error_title", "请输入标题");
			flag = false;
		}
		if(title.length() > 60) {
			model.put("error_title", "标题长度：1-60个字符");
			flag = false;
		}
		
		if( subjectID == 0){
			model.put("error_subjectID", "请选择科目");
			flag = false;
		}
		
		DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start_time = null;
		Date end_time = null;

		if( start_time_str == null || "".equals(start_time_str) ){
			model.put("error_start_time", "请输入开始时间");
			flag = false;
		}else{
			try {
				start_time = f.parse(start_time_str);
			} catch(Exception e){
				model.put("error_start_time", "开始时间格式不正确");
				flag = false;
			}
		}
		
		if( end_time_str == null || "".equals(end_time_str) ){
			model.put("error_end_time", "请输入结束时间");
			flag = false;
		}else{
			try {
				end_time = f.parse(end_time_str);
			} catch(Exception e){
				model.put("error_end_time", "结束时间格式不正确");
				flag = false;
			}
		}
		
		try {
			if( (start_time.compareTo(end_time)) >= 0 ){
				model.put("error_start_time", "开始时间应小于结束时间");
				flag = false;
			}
		} catch(Exception e){
			flag = false;
		}
		
		if( remark == null || "".equals(remark) ){
			exam.setRemark(null);
        }else{
        	if(remark.length() > 60) {
    			model.put("error_remark", "备注长度：0-60个字符");
    			flag = false;
    		}
        }
		
		//校验不通过
        if(!flag){
        	model.put("subjectID", subjectID);
        	model.put("start_time_str", start_time_str);
        	model.put("end_time_str", end_time_str);
        	model.put("subjectList", subjectList);
			return new ModelAndView(getFormView(), model);
		}

		exam.setStart_time(start_time);
		exam.setEnd_time(end_time);
		
		boolean isEditSuccess = examService.editUnstartExamInfo(exam, model);
		
		if (isEditSuccess) {
			return new ModelAndView(getSuccessView());
		} else {
			model.put("subjectID", subjectID);
        	model.put("start_time_str", start_time_str);
        	model.put("end_time_str", end_time_str);
			model.put("subjectList", subjectList);
			return new ModelAndView(getFormView(), model);
		}
	}

}
